# !/python/TextResuming/core/util.py
# util function
# Author : @ismailsunni

import operator

# default values
debug_mode = 1

def debug(message, mode=debug_mode):
	'''debug mode.'''

	if mode == 1:
		print '__debug__', str(message)

# getter
def get_max_value_index(the_list):
	'''Return the index of max value.'''

	return the_list.index(max(the_list))
	
def get_from_dictionary(keyword, dictionary, default_value=0):
	'''Getter function for dictionary. If keyword is not in dictionary, return default_value'''

	if keyword in dictionary:
		return dictionary[keyword]
	else:
		return default_value

def sort_dictionary_by_value(dictionary, descending = True, num_returned = -1):
	'''Sort dictionary to a list.

		@parameter
		@dictionary = the dictionary
		@descending = descending if true, otherwise ascending
		@num_returned = number of element returned. default_value = -1, all.'''

	retval = sorted(dictionary.iteritems(), key=operator.itemgetter(1), reverse = descending)
	if num_returned > len(dictionary):
		return retval
	else:
		return retval[0:num_returned]

def get_index_sorted_list(unsorted_list, descending = True):
	'''Sort a list, and return the index of sorted list.'''

	return sorted(range(len(unsorted_list)), key = lambda k: unsorted_list[k], reverse = descending)

def delete_dict_by_value(dictionary, value):
	'''Delete all element in dictionary by value.'''

	retval = {}
	for key in dictionary:
		if dictionary[key] != value:
			retval[key] = dictionary[key]

	return retval

# converter
def convert_list_of_tuple_to_dictionary(list_of_tuple):
	'''Convert list of tuple to dictionary.'''

	retval = dict()
	for tup in list_of_tuple:
		retval[tup[0]] = tup[1]
		
	return retval

# print
def print_index_list_dict(list_or_dict):
	'''Print index and element of a list or dictionary.'''

	for index, item in enumerate(list_or_dict):
		print index, item

# read external file
def read_text_file(file_path):
	'''Read external text file.

		Return list of string in each line.'''
		
	try:
		f = open(file_path)
		list_of_string = f.readlines()
		f.close()

		return list_of_string
	except Exception, e:
		debug('read_text_file' + str(e))
		return None

def load_yaml_file(yaml_file_path):
	'''Load YAML file.'''

	import yaml
	
	try:
		f = open(yaml_file_path)
		data = yaml.load(f)
		f.close()

		return data

	except Exception, e:
		raise e
		return None

# Write to external file
def write_list(file_path, _list, enter = True):
	'''Write _list to external file. Each element in one line.'''

	try:
		f = open(file_path, 'w')
		for item in _list:
			if enter:
				print >> f, item
			else:
				f.write(item)
		f.close()
		return True
	except Exception, e:
		raise e
		return False

#main for testing
def main():
	file_path = "a.txt"
	b = [1, 2, 3, 4, '\n' 's']
	if write_list(file_path, b):
		print 'True'
	else:
		print'False'

if __name__ == '__main__':
	main()